﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public class SheepManagementService : ISheepManagementService
    {

        private readonly IBaseRepository<SheepManagement> repository;

        public SheepManagementService(IBaseRepository<SheepManagement> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 羊场添加
        /// </summary>
        /// <param name="sheepManagement"></param>
        /// <returns></returns>
        
        public async Task<int> AddSheepManagement(SheepManagement sheepManagement)
        {
            return repository.Add(sheepManagement);
        }

      

        /// <summary>
        /// 羊场删除
        /// </summary>
        /// <param name="sheepManagementId"></param>
        /// <returns></returns>
        public int DelSheepManagement(int sheepManagementId)
        {
            return repository.Del(sheepManagementId);
        }

        /// <summary>
        /// 羊场反填
        /// </summary>
        /// <param name="sheepManagementId"></param>
        /// <returns></returns>
        public SheepManagement GetFtSheepManagement(int sheepManagementId)
        {
            return repository.GetValue(sheepManagementId);
        }

        /// <summary>
        /// 羊场显示
        /// </summary>
        /// <param name="SheepYardName"></param>
        /// <param name="SheepDuty"></param>
        /// <returns></returns>
        public async Task<PageOutput<SheepManagement>> GetSheepManagement(string? SheepYardName,string? SheepDuty, int pageIndex, int pageSize)
        {
            var list = repository.GetAll().ToList();
            if (!string.IsNullOrEmpty(SheepYardName))
            {
                list = list.Where(c => c.SheepYardName.Contains(SheepYardName)).ToList();
            }
            if (!string.IsNullOrEmpty(SheepDuty))
            {
                list = list.Where(c => c.SheepDuty.Contains(SheepDuty)).ToList();
            }
            var result = new PageOutput<SheepManagement>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.SheepManagementId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
        }

        public async Task<int> UpdaSheepManagement(SheepManagement sheepManagement)
        {
            return repository.Update(sheepManagement);
        }

        
    }
}
